// react-dom包的打包配置
import {getPackageJSON, resolvePkgPath, getBaseRollupPlugins} from './utils';
import generatePackageJson from 'rollup-plugin-generate-package-json';
import alias from '@rollup/plugin-alias';

const {name, module, peerDependencies} = getPackageJSON('react-dom');

// react-dom包路径
const pkgPath = resolvePkgPath(name);
// react-dom包产物路径
const pkgDistPath = resolvePkgPath(name, true);

export default [
  // react-dom包
  {
    input: `${pkgPath}/${module}`, // 打包入口地址
    output: [
      // 这个是为了兼容react18之前做的打包入口
      {
        file: `${pkgDistPath}/index.js`,
        name: 'ReactDOM',
        format: 'umd',
      },
      // 这个是针对React18做的打包入口
      {
        file: `${pkgDistPath}/client.js`,
        name: 'client',
        format: 'umd',
      },
    ],
    // 通过external配置打包时，需要排除的依赖包
    external: [...Object.keys(peerDependencies)],
    plugins: [
      ...getBaseRollupPlugins(),
      // 这个alias插件在alias中提供webpack 的resolve中的alias功能（配置打包时的路径别名）
      alias({
        entries: {
          hostConfig: `${pkgPath}/src/hostConfig.ts`,
        },
      }),
      generatePackageJson({
        inputFolder: pkgPath,
        outputFolder: pkgDistPath,
        // 定义打包后的package.json中的内容
        baseContents: ({name, description, version}) => ({
          name,
          description,
          version,
          peerDependencies: {
            react: version,
          },
          main: 'index.js',
        }),
      }),
    ],
  },
];
